package com.guochenglang.www.service.impl;

import com.guochenglang.www.dao.UserDao;
import com.guochenglang.www.pojo.User;
import com.guochenglang.www.service.anotate.Affair;
import com.guochenglang.www.service.UserService;
import com.guochenglang.www.util.Encryption;
import com.guochenglang.www.util.GetTable;
import com.guochenglang.www.util.MyDependency;
import com.guochenglang.www.util.lru.MyCapacity;

import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.ArrayList;


/**
 * 用户业务
 *
 * @author GuoChenglang
 */
public class UserServiceImpl implements UserService {
    private static final UserDao USER_DAO = MyDependency.returnObject(UserDao.class);

    /**
     * 注册
     *
     * @param user 注册的用户
     * @return 主键
     */
    @Affair
    @Override
    public int signIn(User user) throws SQLException, InterruptedException, IllegalAccessException, NoSuchAlgorithmException, InstantiationException {
        //判断用户名是否已经存在
        String username = user.getUsername();
        User condition = new User();
        condition.setUsername(username);
        ArrayList<User> users = USER_DAO.userSelect(condition);
        if (!users.isEmpty()) {
            return -1;
        }
        user.setPassword(Encryption.encryption(user.getPassword()));
        return USER_DAO.insertUser(user);
    }

    /**
     * 修改密码
     *
     * @param newPassword 修改后的用户密码
     * @param username    修改前的用户信息
     * @return 修改行数
     */
    @Affair
    @Override
    public int changePassword(String newPassword, String username) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException, NoSuchAlgorithmException {
        User set = new User();
        set.setPassword(Encryption.encryption(newPassword));
        User myUser = new User();
        myUser.setUsername(username);
        return USER_DAO.updateUser(set, myUser);
    }

    /**
     * 根据id获得具体用户
     *
     * @param id 用户id
     * @return 用户
     */
    @Override
    public User getUser(Integer id) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        String key = MyCapacity.getKey(id, GetTable.getTable(User.class));
        //从缓存获取
        User user = (User) MyCapacity.get(key);
        if (user == null) {
            User condition = new User();
            condition.setId(id);
            ArrayList<User> select = USER_DAO.userSelect(condition);
            if (!select.isEmpty()) {
                user = select.get(0);
                //存入缓存
                MyCapacity.put(key, user);
            }
        }
        return user;
    }

    /**
     * 更新用户信息
     *
     * @param userId 用户id
     * @param set    更新的数据
     * @return 更新行数
     */
    @Affair
    @Override
    public int updateUser(Integer userId, User set) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        String key = MyCapacity.getKey(userId, GetTable.getTable(User.class));
        User myUser = new User();
        myUser.setId(userId);
        //更新user
        int num = USER_DAO.updateUser(set, myUser);
        User condition = new User();
        condition.setId(userId);
        //更新缓存
        MyCapacity.put(key, USER_DAO.userSelect(condition));
        return num;
    }

    /**
     * 登录
     *
     * @param user 登录信息
     * @return 用户
     */
    @Override
    public User logIn(User user) throws SQLException, IllegalAccessException, InstantiationException, InterruptedException, NoSuchAlgorithmException {
        //加密密码
        user.setPassword(Encryption.encryption(user.getPassword()));
        ArrayList<User> select = MyDependency.returnObject(UserDao.class).userSelect(user);
        if (select.isEmpty()) {
            return null;
        }
        return select.get(0);
    }

    /**
     * 根据条件获取对象
     *
     * @param condition 条件
     * @return user对象
     */
    @Override
    public ArrayList<User> getUser(User condition) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        return USER_DAO.userSelect(condition);
    }

}
